package cn.wolfcode.shop.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;

import cn.wolfcode.shop.domain.Catalog;
import cn.wolfcode.shop.domain.Property;
import cn.wolfcode.shop.mapper.CatalogMapper;
import cn.wolfcode.shop.mapper.PropertyMapper;
import cn.wolfcode.shop.service.ICatalogService;
import cn.wolfcode.shop.util.RedisConstance;

/**
 * service 是dubbo的,配置之后不能生效,所以需要使他能够被扫描到,在application配置扫描范围
 * @author stonenic
 *
 */
@Service
@Transactional
public class CatalogServiceImpl implements ICatalogService {

	@Autowired
	private CatalogMapper catalogMapper;
	@Autowired
	private PropertyMapper propertyMapper;
	// 封装好了jedis
	@Autowired
	private RedisTemplate redisTemplate;

	@Override
	public List<Catalog> getCataloglist() {
		// 从redis中获取商品分类树(注入redisTemplate)
		ValueOperations opsForValue = redisTemplate.opsForValue();
		List<Catalog> catalogList = (List<Catalog>) opsForValue.get(RedisConstance.CATALOG_ALL);
		// 判断redis中返回结果是否为空,首次获取是空的
		if (catalogList == null) {
			catalogList = reloadCache();
		}
		// 如果空则需要去数据库查询查询商品分类树,并且设置到redis中
		// 否则直接返回redis中获取到的分类树
		return catalogList;
	}

	/**
	 * 更新redis缓存商品分类树的数据
	 * @return
	 */

	private List<Catalog> reloadCache() {
		List<Catalog> catalogList;
		catalogList = catalogMapper.selectAll();
		redisTemplate.opsForValue().set(RedisConstance.CATALOG_ALL, catalogList);
		return catalogList;
	}

	@Override
	public List<Catalog> getChildCatalog(Long id) {
		List<Catalog> childCatalogList = catalogMapper.getChildCatalog(id);
		childCatalogList.forEach(catalog -> {
			// 1.通过当前遍历到的分类id去redis中查找,该分类对应的属性个数
			Integer propertyCount = (Integer) redisTemplate.opsForValue()
					.get(RedisConstance.CATALOG_PROPERTY_COUNT.replace("ID", catalog.getId() + ""));
			// 2.如果属性个数是null,去数据库统计
			if (propertyCount == null) {
				propertyCount = propertyMapper.getPropertyConutByCatalogId(catalog.getId());
				redisTemplate.opsForValue()
						.set(RedisConstance.CATALOG_PROPERTY_COUNT.replace("ID", catalog.getId() + ""), propertyCount);

			}
			// 3.否则将属性个数设置到当前遍历到的分类对象中(给catalog类中增加propertyCouunt属性)
			catalog.setPropertyCount(propertyCount);
		});
		return childCatalogList;
	}

	@Override
	public void sortCatalog(List<Long> ids) {
		for (int i = 0; i < ids.size(); i++) {
			Catalog catalog = catalogMapper.selectByPrimaryKey(ids.get(i));
			catalog.setSort(i + 1);
			catalogMapper.updateByPrimaryKey(catalog);
		}
		// 更新的时候刷新redis缓存
		reloadCache();
	}

	@Override
	public void saveOrUpdate(Catalog catalog) {
		if (catalog.getId() == null) {
			catalogMapper.insert(catalog);
		} else {
			catalogMapper.updateByPrimaryKey(catalog);
		}
		// 更新的时候刷新redis缓存
		reloadCache();
	}

	@Override
	public void delete(Long id) {
		catalogMapper.deleteByPrimaryKey(id);
		// 更新的时候刷新redis缓存
		reloadCache();
	}

	@Override
	public void listChildCataologPerMinute() {

	}

	@Override
	public void listCataologPropertyCountOneHour() {
		List<Catalog> cataloglist = catalogMapper.selectAll();
		cataloglist.forEach(catalog -> {
			List<Catalog> childCatalogList = catalogMapper.getChildCatalog(catalog.getId());
			childCatalogList.forEach(childCatalog -> {
				Integer propertyConut = propertyMapper.getPropertyConutByCatalogId(childCatalog.getId());
				redisTemplate.opsForValue().set(RedisConstance.CATALOG_PROPERTY_COUNT.replace("ID", childCatalog.getId() + ""), propertyConut);
				childCatalog.setProductCount(propertyConut);
			});

		});

	}

}
